home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7754 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.pl1,comp.lang.c
  4. Subject: Re: PL/I and C
  5. Date: 27 Feb 1996 23:32:45 GMT
  6. Organization: Los Alamos National Laboratory
  7. Distribution: world
  8. Message-ID: <TANMOY.96Feb27163245@qcd.lanl.gov>
  9. References: <4gh5ru$eng@goanna.cs.rmit.EDU.AU> <312CCEB2.4AB7@corp.dialog.com>
  10.     <AD536AAB9668B76CD@mcdialb09.it.luc.edu>
  11.     <312E363C.3CDE@corp.dialog.com>
  12.     <AD53AB1396681879FA@mcdialb10.it.luc.edu>
  13.     <4gsgrd$v7u@zk2nws.zko.dec.com>
  14. NNTP-Posting-Host: qcd.lanl.gov
  15. Mime-Version: 1.0
  16. Content-Type: text
  17. In-reply-to: zeeb@jacare.zk3.dec.com's message of 26 Feb 1996 14:41:49 GMT
  18.  
  19. In article <4gsgrd$v7u@zk2nws.zko.dec.com>
  20. zeeb@jacare.zk3.dec.com (Jeff Zeeb DEC C) writes:
  21.  
  22. JZDC: >
  23. JZDC: >   foo *x;
  24. JZDC: >   bar *y;
  25. JZDC: >
  26. JZDC: >   y=(bar *) x;
  27. JZDC: 
  28. JZDC: This is dangerous territory.  The code fragment above violates the
  29. JZDC: aliasing rules in the ANSI C standard.  In a nutshell, those rules
  30. JZDC: state that a pointer to one type will not point to an object of a
  31. JZDC: different type.  An optimizer could generate code using this assumption,
  32. JZDC: which would have unexpected results in your program.
  33.  
  34. Actually that is not quite correct. The relevant guarantee in C is
  35. that an object defined to be of a certain type shall be accessed with
  36. an lvalue of a compatible type, or by an lvalue of a character
  37. type. (Actually, the rules are a bit more precise: qualified and
  38. unqualified versions of a type are allowed, as are access as a part of
  39. a union, array or struct whose elements/fields contains the actual
  40. type). 
  41.  
  42. For example, if all that the compiler saw was the following:
  43.  
  44. long xx = 0;
  45. short yy = 0;
  46. long *x = &xx; 
  47. short *y;
  48. y = (short*) x; 
  49. f(y); 
  50.  
  51. It cannot assume that xx is unchanged after the call. The reason is
  52. that f may do a *(long*)y and hence change y.
  53.  
  54. On the other hand, if the `f(y);' were replaced by
  55.  
  56. *y = yy;
  57.  
  58. The compiler could have assumed xx hasn't changed! The reason is that
  59. *y is of type short, and is not allowed to be used to change xx which
  60. is of type long.
  61.  
  62. On the other hand, if short was changed to char consistently above,
  63. again, the compiler had to play safe. A character lvalue can be used
  64. to change a long.
  65.  
  66. Cheers
  67. Tanmoy
  68. --
  69. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  70. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  71. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  72. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  73. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  74. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  75.